package sort;

/**
 * @author Europa
 * @create 2020-05-18 15:56
 */
public class Cocktail extends Sort {

    @Override
    public void sort(int[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            boolean isSorted = true;
            for (int j = i; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    exchange(array, j, j + 1);
                    isSorted = false;
                }
            }
            if (isSorted) {
                break;
            }
            isSorted = true;
            for (int j = array.length - i - 1; j >= 1; j--) {
                if (array[j] < array[j - 1]) {
                    exchange(array, j, j - 1);
                    isSorted = false;
                }
            }
            if (isSorted) {
                break;
            }
        }
    }
}
